﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace MT.CustomUserControl.ViewModels
{
    public class TwoComboxUcViewModel:ObservableObject
    {
        #region 第 1 个Combox

        public Action<int> Combox_1_SelectedChangedActon;

        private int combox_1_CurSelectedIndex = -1;
        public int Combox_1_CurSelectedIndex
        {
            get { return combox_1_CurSelectedIndex; }
            set 
            {
                if (combox_1_CurSelectedIndex != value)
                {
                    SetProperty(ref combox_1_CurSelectedIndex, value);
                    if (combox_1_CurSelectedIndex != -1)
                        Combox_1_SelectedChangedActon?.Invoke(combox_1_CurSelectedIndex);
                }                
            }
        }

        private string combox_1_CurSelectedValue = string.Empty;
        public string Combox_1_CurSelectedValue
        {
            get { return combox_1_CurSelectedValue; }
            set { SetProperty(ref combox_1_CurSelectedValue, value); }
        }

        private ObservableCollection<string> comboxItemsSource1 = new ObservableCollection<string>();
        public ObservableCollection<string> ComboxItemsSource1
        {
            get { return comboxItemsSource1; }
            set 
            { 
                SetProperty(ref comboxItemsSource1, value);
                if (ComboxItemsSource1 != null && ComboxItemsSource1.Count > 0)
                {
                    ComboxItemsSourceListBack.Clear();
                    foreach (var item in ComboxItemsSource1)
                    {
                        ComboxItemsSourceListBack.Add(item);
                    }
                }
            }
        }

        /// <summary>
        /// 选项备份，用于在其他线程调用
        /// </summary>
        public List<string> ComboxItemsSourceListBack = new List<string>();

        #endregion

        #region 第 2 个Combox    
        public Action<int> Combox_2_SelectedChangedActon;

        private int combox_2_CurSelectedIndex = -1;
        public int Combox_2_CurSelectedIndex
        {
            get { return combox_2_CurSelectedIndex; }
            set
            {
                if (combox_2_CurSelectedIndex != value)
                {
                    SetProperty(ref combox_2_CurSelectedIndex, value);
                    if (combox_2_CurSelectedIndex != -1)
                        Combox_2_SelectedChangedActon?.Invoke(combox_2_CurSelectedIndex);
                }
            }
        }

        private string combox_2_CurSelectedValue = string.Empty;
        public string Combox_2_CurSelectedValue
        {
            get { return combox_2_CurSelectedValue; }
            set { SetProperty(ref combox_2_CurSelectedValue, value); }
        }

        private ObservableCollection<string> comboxItemsSource2 = new ObservableCollection<string>();
        public ObservableCollection<string> ComboxItemsSource2
        {
            get { return comboxItemsSource2; }
            set { SetProperty(ref comboxItemsSource2, value); }
        }

        private Visibility combox2Visibility = Visibility.Visible;
        public Visibility Combox2Visibility
        {
            get { return combox2Visibility; }
            set { SetProperty(ref combox2Visibility, value); }
        }

        #endregion

        public TwoComboxUcViewModel(Action<int> action1, Action<int> action2, Visibility combox2Visibility = Visibility.Visible)
        {
            Combox_1_SelectedChangedActon = action1;
            Combox_2_SelectedChangedActon = action2;
            Combox2Visibility = combox2Visibility;
        }

        public void InitAndReset()
        {
            Combox_1_CurSelectedValue = string.Empty;
            Combox_2_CurSelectedValue = string.Empty;
        }
    }
}
